﻿using System;
using System.Web;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfMyRateMana : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                doShowRateGrid("", "", "", "", "", "", "");
            }

        }
        private void BindControls()
        {
            if (!IsPostBack)
            { 
            }
        }


        private void doShowRateGrid(string strRateDtS, string strRateDtE, string strUserNme, string strGoodsNme
            , string CurrPageNum, string strPageRowsCount, string strSort)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpOrderRate oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpOrderRate())
            {
                string strSql = @"
    SELECT
    A.*
    ,B.NORMS_DESC
    ,B.IMG_URL 
    ,B.GOODS_NME 
    ,C.COMM_TENANT_NME 
    FROM SHP_ORDER_RATE A 
    LEFT OUTER JOIN SHP_ORDER_FORM_LIST B ON A.ORDER_LIST_ID=B.ORDER_LIST_ID
    LEFT OUTER JOIN SHP_COMMERCIAL_TENANT_INFO C ON A.COMM_TENANT_ID=C.COMM_TENANT_ID
    WHERE
    (A.USER_NME='" + this.oUserInfo.Operator + @"')
    ";
                if (strUserNme != string.Empty)
                {
                    strSql += " AND (A.COMM_USER_NME ='" + strUserNme.Replace("'", "") + "' OR C.COMM_TENANT_NME LIKE '%" + strUserNme.Replace("'", "") + "%')";
                }
                else if (strGoodsNme != string.Empty)
                {
                    strSql += " AND B.GOODS_NME like '%" + strGoodsNme.Replace("'","") + "%'";
                }
                if (strRateDtS != string.Empty)
                {
                    strSql += " AND (DATADIFF(DAY,A.BUYER_RATE_DT1,'" + strRateDtS + "') <=0)";
                }
                if (strRateDtE != string.Empty)
                {
                    strSql += " AND (DATADIFF(DAY,A.BUYER_RATE_DT1,'" + strRateDtE + "') >=0)";
                }

                DataSet ds = oCDbAccess.DataObj.QueryBySQL(strSql);
                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", ""
                    , (CurrPageNum == string.Empty ? 1 : int.Parse(CurrPageNum))
                    , (strPageRowsCount == string.Empty ? 20 : int.Parse(strPageRowsCount))
                    , (strSort == string.Empty ? "BUYER_RATE_DT1 DESC" : strSort));
                this.ExecJScript(@" var data= " + strjsontem1 + @" ; 
                updateRateTableHtml( data);");
            }
        }

        private void doSellerReply(string strOrderListId)
        {
            if (this.GetParamHtValue("BUYER_RATE_DESC2") == string.Empty)
            {
                throw new Exception("评价内容不能为空！");
            }
            using (msBsc.AppLogic.DataAlc.CBizShpOrderRate oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpOrderRate())
            {
                DataSet ds = oCDbAccess.SelectById(strOrderListId);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["USER_NME"].ToString().ToUpper() != this.oUserInfo.Operator.ToUpper())
                    {
                        throw new Exception("只能评价自己购买的订单！"); 
                    }
                    if (ds.Tables[0].Rows[0]["BUYER_RATE_DT2"].ToString() == string.Empty)
                    {
                        ds.Tables[0].Rows[0]["BUYER_RATE_DESC2"] = this.GetParamHtValue("BUYER_RATE_DESC2");
                        ds.Tables[0].Rows[0]["BUYER_RATE_DT2"] = System.DateTime.Now;
                        oCDbAccess.Update(ds);
                    }
                    else
                    {
                        throw new Exception("已经追加过评价，不能重复评价！");
                    }
                    this.ExecJScript("layer.close(layerform);doRateToPageNm();");
                }
            }
        }
          
        public override void DoAction()
        {
            if (this.strAction == "doFinderRateList")
            {
                doShowRateGrid(
                    this.GetParamHtValue("INITIAL_DT_S")
                    , this.GetParamHtValue("INITIAL_DT_E")
                    , this.GetParamHtValue("USER_NME")
                    , this.GetParamHtValue("GOODS_NME") 
                    , this.GetParamHtValue("NEW_PAGE_NUM")
                    , this.GetParamHtValue("PAGE_ROW_COUNT")
                    , this.GetParamHtValue("PAGE_ROW_SORT"));
            }
            else if (this.strAction == "doSellerReply")
            {
                doSellerReply(this.GetParamHtValue("ORDER_LIST_ID") );
            }

        }
    }
}